
<div class="gid_13_table_title" style="display:block">用户</div>
<div class="gid_13_table_content">
    <div class="layui-card gid_13">
        <!-- 搜索表单区 -->
        <form class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="gid-13-tno-2-sys_user-search-form">
            <div class="layui-form-item">
            #set(initValue = 'org_id' == filterKey?filterValue:'')
            #set(disabled = 'org_id'.equals(filterKey))

            <!-- 所属机构组件类型：select -->
                        <!-- 下拉表单 -->
                        <div class="layui-inline">
                            <label class="layui-form-label">所属机构</label>
                            <div class="layui-input-inline">
                                <div id="gid-13-tno-2_org_id_search_select" name="org_id"></div>
                            </div>
                        </div>

            #set(initValue = 'user_name' == filterKey?filterValue:'')
            #set(disabled = 'user_name'.equals(filterKey))

            <!-- 用户姓名组件类型：text -->
                    <div class="layui-inline">
                        <label class="layui-form-label">用户姓名</label>
                        <div class="layui-input-inline">
                            <input type="text" name="user_name__op__like" value="#(initValue)" #(disabled?'disabled':'') placeholder="请输入用户姓名" class="layui-input" autocomplete="off">
                        </div>
                    </div>

            #set(initValue = 'user_sex' == filterKey?filterValue:'')
            #set(disabled = 'user_sex'.equals(filterKey))

            <!-- 性别组件类型：select -->
                        <!-- 下拉表单 -->
                        <div class="layui-inline">
                            <label class="layui-form-label">性别</label>
                            <div class="layui-input-inline">
                                <select id="gid-13-tno-2_user_sex_search_select" name="user_sex" xm-select-search="" xm-select-radio=""
                                    placeholder="请选择性别" autocomplete="off" xm-select-height="36px" xm-select-skin="normal" xm-select="gid-13-tno-2_user_sex_search_select" lay-filter="gid-13-tno-2_user_sex_search_select">
                                    <option value="">请选择性别</option>
                                </select>
                            </div>
                        </div>

            #set(initValue = 'user_account' == filterKey?filterValue:'')
            #set(disabled = 'user_account'.equals(filterKey))

            <!-- 登录账号组件类型：text -->
                    <div class="layui-inline">
                        <label class="layui-form-label">登录账号</label>
                        <div class="layui-input-inline">
                            <input type="text" name="user_account" value="#(initValue)" #(disabled?'disabled':'') placeholder="请输入登录账号" class="layui-input" autocomplete="off">
                        </div>
                    </div>

            #set(initValue = 'user_phone' == filterKey?filterValue:'')
            #set(disabled = 'user_phone'.equals(filterKey))

            <!-- 手机号组件类型：text -->
                    <div class="layui-inline">
                        <label class="layui-form-label">手机号</label>
                        <div class="layui-input-inline">
                            <input type="text" name="user_phone" value="#(initValue)" #(disabled?'disabled':'') placeholder="请输入手机号" class="layui-input" autocomplete="off">
                        </div>
                    </div>

            #set(initValue = 'user_email' == filterKey?filterValue:'')
            #set(disabled = 'user_email'.equals(filterKey))

            <!-- 电子邮箱组件类型：text -->
                    <div class="layui-inline">
                        <label class="layui-form-label">电子邮箱</label>
                        <div class="layui-input-inline">
                            <input type="text" name="user_email" value="#(initValue)" #(disabled?'disabled':'') placeholder="请输入电子邮箱" class="layui-input" autocomplete="off">
                        </div>
                    </div>

            #set(initValue = 'user_status' == filterKey?filterValue:'')
            #set(disabled = 'user_status'.equals(filterKey))

            <!-- 用户状态组件类型：select -->
                        <!-- 枚举表单 -->
                        <div class="layui-inline">
                            <label class="layui-form-label">用户状态</label>
                            <div class="layui-input-inline">
                                <select name="user_status" #(disabled?'disabled':'') lay-verify="">
                                    <option value="">请选择</option>
                                    #set(initValue = ('Integer' == 'Boolean')? (initValue == true?'1':(initValue == false?'0':initValue)) : initValue)
                                    <option value="1" #('1'==initValue? 'selected':'')>启用</option>
                                    <option value="2" #('2'==initValue? 'selected':'')>禁用</option>
                                </select>
                            </div>
                        </div>

            #set(initValue = 'create_datetime' == filterKey?filterValue:'')
            #set(disabled = 'create_datetime'.equals(filterKey))

            <!-- 创建时间组件类型：datetime -->
                    <div class="layui-inline">
                        <label class="layui-form-label">创建时间</label>
                        <div class="layui-input-inline">
                            <input type="text" name="create_datetime__op__close_range" id="gid-13-tno-2-create_datetime_search_datetime" class="layui-input" placeholder="yyyy-MM-dd HH:mm:ss"  value="#if(isDateObject(initValue)) #date(initValue, 'yyyy-MM-dd HH:mm:ss') #else #(initValue) #end">
                        </div>
                    </div>
            #set(initValue = 'user_last_login_datetime' == filterKey?filterValue:'')
            #set(disabled = 'user_last_login_datetime'.equals(filterKey))

            <!-- 最近登录组件类型：datetime -->
                    <div class="layui-inline">
                        <label class="layui-form-label">最近登录</label>
                        <div class="layui-input-inline">
                            <input type="text" name="user_last_login_datetime__op__close_range" id="gid-13-tno-2-user_last_login_datetime_search_datetime" class="layui-input" placeholder="yyyy-MM-dd HH:mm:ss"  value="#if(isDateObject(initValue)) #date(initValue, 'yyyy-MM-dd HH:mm:ss') #else #(initValue) #end">
                        </div>
                    </div>
                <div class="layui-inline">
                    <button class="layui-btn" lay-submit lay-filter="gid-13-tno-2-btn-sys_user-form-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
        <!-- 列表区 -->
        <div class="layui-card-body">

            <!-- 头部工具栏模板 -->
            <script type="text/html" id="sys_user_header_toolbar">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
                    <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="dels">删除</button>
                </div>
            </script>

            <!-- 列表表格 -->
            <table id="gid-13-tno-2-sys_user-table" lay-filter="gid-13-tno-2-sys_user-table" style="width: 100%"></table>

            <!-- 每行操作按钮模板 -->
            <script type="text/html" id="gid-13-tno-2-sys_user_row_toolbar">
                <a class="layui-btn layui-btn-default layui-btn-xs" lay-event="edit" title="编辑"><i class="layui-icon layui-icon-edit table_btn_icon"></i></a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" title="删除"><i class="layui-icon layui-icon-delete table_btn_icon"></i></a>
            </script>
        </div>
    </div>
</div>
<!-- JS脚本区 -->
<script>

    layui.use(['table', 'form', 'layer', 'laydate', 'jquery', 'admin', 'number', 'xSelects'], function () {
        var form = layui.form, layer = layui.layer, laydate = layui.laydate, table = layui.table, admin = layui.admin, number = layui.number, xSelects = layui.xSelects;

        form.render(null, 'gid-13-tno-2-sys_user-search-form');

                    var initValue = '#('org_id' == filterKey?filterValue:'')';
                    #set(disabled = 'org_id'.equals(filterKey))
                    //渲染所属机构下拉分页选择器
                    xSelects.render({
                        selectId: 'gid-13-tno-2_org_id_search_select',
                        url: '/system/org/table2/org_id/sql/list',
                        titleKey: 'org_name',
                        valueKey: 'org_id',
                        searchKey: 'org_name__op__like',
                        showPage: true,
                        size: 10,
                        multiSelect: false,
                        model: { type: 'fixed' },
                        zIndex: 1000000000,
                        disabled: #(disabled),
                        init: initValue!=''?[{val:initValue}]:[],
                        tree: {
                            pidKey: 'org_pid',
                            pidFilterParamName: 'org_pid__op__eq',
                            expandedKeys:true,
                            lazy: false,
                        },
                        // data: [
                        //     {
                        //         name: '请选择',
                        //         value: 0,
                        //         disabled: true,
                        //         children: []
                        //     }
                        // ],
                        // 初始值设置
                        initValues: initValue!=''?[initValue]:[]
                    });

                    var initValue = '#('user_sex' == filterKey?filterValue:'')';
                    //渲染性别下拉分页选择器
                    xSelects.render({
                      selectId: 'gid-13-tno-2_user_sex_search_select',
                      url: '/system/dic/list?dic_pid=15&valueKey=dic_value',
                      titleKey: 'dic_name',
                      valueKey: 'dic_value',
                      searchKey: 'dic_name__op__like',
                      showPage: true,
                      size: 10,
                      multiSelect: false,
                      model: { type: 'fixed' },
                      zIndex: 1000000000,
                      // 初始值设置
                      initValues: initValue!=''?[initValue]:[],
                    });

                //渲染创建时间日期时间选择器
                laydate.render({ elem: '#gid-13-tno-2-create_datetime_search_datetime' ,type: 'datetime', range: true });

                //渲染最近登录日期时间选择器
                laydate.render({ elem: '#gid-13-tno-2-user_last_login_datetime_search_datetime' ,type: 'datetime', range: true });
        // 表单搜索监听
        form.on('submit(gid-13-tno-2-btn-sys_user-form-search)', function (data) {
            console.log('gid-13-tno-2-sys_user-search-form', data)
            var field = data.field;
            #if(notBlank(filterKey))
            field = { ...field, '#(filterKey)': '#(filterValue)' }
            #end
            //执行重载
            table.reload('gid-13-tno-2-sys_user-table', {
                where: field,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false;
        });
        // 渲染表格数据
        var tableIns = table.render({
            id: 'gid-13-tno-2-sys_user-table',
            elem: '#gid-13-tno-2-sys_user-table',
            // 树表格专用参数
            tree: {
                iconIndex: 1,
                isPidData: true,
                idName: 'user_id',
                pidName: '',
                arrowType: 'arrow2',
                onlyIconControl: true,
            },
            toolbar: '#sys_user_header_toolbar',
            url: '/system/org/table2/list',
            #if(notBlank(filterKey))
            where: { #(filterKey): "#(filterValue)" },
            #end
            loading: true,
            page: true, //开启分页
            cols: [[
                {type: 'checkbox', fixed: 'left'},

                {field: 'org_id', title: '所属机构', templet: function (row){
                    if(row.org_id_title){
                        return row.org_id_title;
                    }
                    return '--';
                }},

                    {field: 'user_name', title: '用户姓名'},

                {field: 'user_sex', title: '性别', templet: function (row){
                    if(row.user_sex_title){
                        return row.user_sex_title;
                    }
                    return '--';
                }},

                    {field: 'user_account', title: '登录账号'},

                    {field: 'user_phone', title: '手机号'},

                    {field: 'user_email', title: '电子邮箱'},

                {field: 'user_status', title: '用户状态', templet: function (row){
                    let enumList = [{"title":"启用","value":"1","no":"1"},{"title":"禁用","value":"2","no":"2"}];
                    let value = row.user_status;
                    for(let idx=0;idx<enumList.length;idx++){
                        if(enumList[idx].value == value) {
                            return enumList[idx].title;
                        }
                    }
                    return '--';
                }},

                    {field: 'user_last_login_datetime', title: '最近登录'},
                    {title: '操作', width: '', fixed: 'right', templet: '#gid-13-tno-2-sys_user_row_toolbar'}
            ]],
            done: function (a,b,c) {
                console.log('tableIns', tableIns);
                tableIns.expandAll&&tableIns.expandAll()
            }
        });

        table['gid-13-tno-2-sys_user-table'] = tableIns;

        // 监听表格工具栏事件
        table.on("toolbar(gid-13-tno-2-sys_user-table)", eventListener)

        // 监听表格行事件
        table.on("tool(gid-13-tno-2-sys_user-table)", eventListener)

                           ;
        // 事件监听
        function eventListener(obj) {
            let event = obj.event;
            let data = obj.data;
            var postData = {};
            #if(notBlank(filterKey))
            postData.filterKey = '#(filterKey)';
            postData.filterValue = '#(filterValue)';
            #end
            switch (event) {
                case 'addSub':
                    postData.filterKey = '';
                    postData.filterValue = data.user_id;
                case 'add':
                    layui.$.post('/system/org/table2/editPage', postData, function (str) {
                        layer.open({
                            type: 1,
                            title: '新建用户',
                            maxmin: true,
                            area: ['800px', '694px'],
                            content: str,
                        })
                    });
                    break;
                case 'edit':
                    postData.user_id = data.user_id;
                    layui.$.post('/system/org/table2/editPage', postData, function (str) {
                        layer.open({
                            type: 1,
                            title: '编辑用户',
                            maxmin: true,
                            area: ['800px', '694px'],
                            content: str,
                        })
                    });
                    break;
                case 'dels':
                    let checkStatus = table.checkStatus('gid-13-tno-2-sys_user-table');
                    let selectRow = checkStatus.data;
                    deleteRow(selectRow);
                    break;
                case 'del':
                    let row = obj.data;
                    deleteRow([row]);
                    break;
            }
            console.log('toolbar_event', obj)
        }

        // 删除行方法
        function deleteRow(selectRows) {
            if (selectRows.length <= 0) {
                layer.msg('请先钩选要删除的记录行！');
                return;
            }
            layer.confirm('确定要删除这' + selectRows.length + '条记录吗？',
                {icon: 3, title: '提示', btn: ['确认删除', '取消']},
                function (index) {
                    console.log(index);
                    layui.$.ajax({
                        url: '/system/org/table2/delete',
                        data: {
                            ids: selectRows.map((row) => row.user_id)
                        },
                        traditional: true,
                        type: 'POST',
                        success: function (result) {
                            if (result.success) {
                                layer.msg('删除成功', {icon: 6});
                                table.reload('gid-13-tno-2-sys_user-table');
                            } else {
                                layer.msg('删除失败！' + result.message, {icon: 5});
                            }
                        }
                    });
                });
        }
    })
</script>